#include "programset.h"
#include "programengine.h"
#include "regdata.h"
#include "modbustartinstruction.h"
#include "modbusstartinstructionlogic.h"
#include <QSettings>
#include "modbusmanager.h"
#include "GeneralDefine.h"
#include "messagelog.h"

extern ModbusManager *modbusManager;

ModbusStartInstructionLogic::ModbusStartInstructionLogic(int robotIdIn, ProgramSet *programSetIn, ProgramEngine *programEngineIn, RegData *regDataIn)
{

    isInitialModbusDevice = false;

    robotId = robotIdIn;
    programSet = programSet;
    programEngine = programEngineIn;
    regData = regDataIn;
    isInitialModbusDevice = true;

}

E_PROGRAM_LOOP_RESULT ModbusStartInstructionLogic::startModbusStart(int programNumIn,
                                       const ModbusStartInstruction &instructionIn, int lineIndex)
{
    Q_UNUSED(lineIndex);
    if(isInitialModbusDevice == false)
    {
        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12080, robotId, programEngine->getRunningLineFloat());
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    if(instructionIn.instructionType != MODBUS_START)
    {
        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12081, robotId, programEngine->getRunningLineFloat());
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    // Modbus-Rs485主机模式
    if(instructionIn.modbusType == E_MODBUS_SPEC_TYPE_RS485_MASTER)
    {

        if(1!=start_Rs485Master())
        {
            addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12083, robotId, programEngine->getRunningLineFloat());
            return E_PROGRAM_LOOP_RESULT_ERROR;
        }
    }
    // Modbus-Rs485从机模式
    else if(instructionIn.modbusType == E_MODBUS_SPEC_TYPE_RS485_SLAVE)
    {

        if(1!=start_Rs485Slave())
        {
            addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12084, robotId, programEngine->getRunningLineFloat());
            return E_PROGRAM_LOOP_RESULT_ERROR;
        }
    }
    // Modbus-TCP主机模式
    else if(instructionIn.modbusType == E_MODBUS_SPEC_TYPE_INTERNET_MASTER)
    {

        if(1!=start_InternetMaster())
        {
            addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12085, robotId, programEngine->getRunningLineFloat());
            return E_PROGRAM_LOOP_RESULT_ERROR;
        }

    }
    // Modbus-TCP从机模式
    else if(instructionIn.modbusType == E_MODBUS_SPEC_TYPE_INTERNET_SLAVE)
    {

        if(0>start_InternetSlave(instructionIn.busId))
        {
            addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12086, robotId, programEngine->getRunningLineFloat());
            return E_PROGRAM_LOOP_RESULT_ERROR;
        }
    }
    // ERROR
    else if(instructionIn.modbusType == E_MODBUS_SPEC_TYPE_ERROR)
    {

        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12082, robotId, programEngine->getRunningLineFloat());
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    // 创建出Modbus成功
    programEngine->calculateNextLineToRun(programNumIn);
    return E_PROGRAM_LOOP_RESULT_NORMAL;
}



int ModbusStartInstructionLogic::initialModbusDevice()
{

    return 1;
}

int ModbusStartInstructionLogic::start_Rs485Master()
{
    return 1;
}

int ModbusStartInstructionLogic::start_Rs485Slave()
{
    return -1;
}

int ModbusStartInstructionLogic::start_InternetMaster()
{
    return 1;
}

int ModbusStartInstructionLogic::start_InternetSlave(int portIn)
{
      return modbusManager->createModbusTcpServer(portIn);
}

int ModbusStartInstructionLogic::readRs485MasterConfig(const QString &filePath)
{

    return 1;
}

int ModbusStartInstructionLogic::readRs485SlaveConfig(const QString &filePath)
{

    return 1;
}

int ModbusStartInstructionLogic::readInternetMasterConfig(const QString &filePath)
{

}

int ModbusStartInstructionLogic::readInternetSlaveConfig(const QString &filePath)
{

}

void ModbusStartInstructionLogic::addMsg(int messageLevel, string componentName, string messageType, int messageCode, int robotId, int parameter1, int parameter2, int parameter3, int parameter4, QString message)
{
    Q_UNUSED(parameter2);
    Q_UNUSED(parameter3);
    Q_UNUSED(parameter4);

    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;

    QString infomationStr;

    switch(messageCode)
    {
   case 12080:
   {
        infomationStr = QObject::tr("机器人:") +
            QString::number(robotId) +
            QObject::tr("第") +
            QString::number(parameter1+1) +
            QObject::tr("行运行出现异常:Modbus设备初始化失败!")+message;
        break;
   }
   case 12081:
   {
        infomationStr = QObject::tr("机器人:") +
            QString::number(robotId) +
            QObject::tr("第") +
            QString::number(parameter1+1) +
            QObject::tr("行运行出现异常:ModbusStart指令类型错误!")+message;
        break;
   }
    case 12082:
    {
         infomationStr = QObject::tr("机器人:") +
             QString::number(robotId) +
             QObject::tr("第") +
             QString::number(parameter1+1) +
             QObject::tr("行运行出现异常:ModbusStart工作模式错误!")+message;
         break;
    }
        default:
       {
            break;
       }
    }
    tmpMsg.MessageInformation = infomationStr.toStdString();
    MessageLog::getInstance()->addMessage(tmpMsg);
}
